package com.lxq.LeetCode.singleNumber;

import java.util.Arrays;

public class Solution {
    public static void main(String[] args) {
        int[] nums = {4, 1, 2, 1, 2};
        System.out.println(singleNumber(nums));
    }

    public static int singleNumber(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        Arrays.sort(nums);
        int p = 0, q = 1;
        while (q < nums.length) {
            if (nums[p] == nums[q]) {
                p = p + 2;
                q = q + 2;
            } else {
                return nums[p];
            }
        }
        return nums[nums.length - 1];
    }
}
